package com.adams.action;

import java.io.File;
import java.util.Map;

import net.sf.json.JSONObject;

import com.adams.action.view.ViewAction;
import com.adams.common.Constants;

/**
 * 菜系
 * 
 * @author 904912
 * 
 */
public class CuisineAction extends DcxtBaseAction {
    private static final long serialVersionUID = -6027608499750316216L;
    // 图标
    private File cuisine_icon;
    private String cuisine_iconFileName;
    private String cuisine_iconContentType;
    private int[] sPixle = new int[] { 32, 32 };
    String bussinessDir = "/dcxt";
    String cuisineCode = "dcxt_cuisine";

    public String addCuisine() throws Exception {
        Map inputMap = uploadPicAndCutImage(cuisine_icon, cuisine_iconFileName, cuisine_iconContentType, true,
                "cuisine_icon", sPixle, bussinessDir, "cuisine");
        Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(), menuId, formPanelId, inputMap);
        if (map.get("success") != null && ((Boolean) map.get("success")) == true) {
            map.put("msg", "添加成功");
        }
        write(JSONObject.fromObject(map).toString());

        return null;
    }

    public String updateCuisine() throws Exception {
        Map inputMap = ViewAction.requestParameterMap2Map(request);
        Map source = dicTableService.getDataByKey(cuisineCode, inputMap);
        String pic = (String) source.get("cuisine_icon");
        if (pic != null && !pic.equals(inputMap.get("cuisine_icon"))) {
            inputMap = uploadPicAndCutImage(cuisine_icon, cuisine_iconFileName, cuisine_iconContentType, true,
                    "cuisine_icon", sPixle, bussinessDir, "cuisine");
            String filename = pic.substring(pic.lastIndexOf("/") + 1);
            File cFile = new File(Constants.adDir + bussinessDir + "/" + filename);
            if(cFile.exists()){
                cFile.delete();
            }
        }
        Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(), menuId, formPanelId, inputMap);
        if (map.get("success") != null && ((Boolean) map.get("success")) == true) {
            map.put("msg", "修改成功");
        }
        write(JSONObject.fromObject(map).toString());
        return null;
    }

    public File getCuisine_icon() {
        return cuisine_icon;
    }

    public void setCuisine_icon(File cuisine_icon) {
        this.cuisine_icon = cuisine_icon;
    }

    public String getCuisine_iconFileName() {
        return cuisine_iconFileName;
    }

    public void setCuisine_iconFileName(String cuisine_iconFileName) {
        this.cuisine_iconFileName = cuisine_iconFileName;
    }

    public String getCuisine_iconContentType() {
        return cuisine_iconContentType;
    }

    public void setCuisine_iconContentType(String cuisine_iconContentType) {
        this.cuisine_iconContentType = cuisine_iconContentType;
    }

}
